package com.jwthhealth_pub.old.diet.view.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.common.ImageLoader;
import com.jwthhealth_pub.old.diet.moudel.DietThiModule;
import com.jwthhealth_pub.old.diet.view.DietForActivity;
import com.jwthhealth_pub.old.diet.view.DietThiActivity;

import java.util.List;

/**
 * Created by Administrator on 2016/12/27.
 */
public class DietthiAdapter extends RecyclerView.Adapter {

    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    private Context mContext;
    private List<DietThiModule.DataBean> data;
    private String title;

    public DietthiAdapter(DietThiActivity dietThiActivity, List<DietThiModule.DataBean> data) {
        this.mContext = dietThiActivity;
        this.data = data;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

//        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_diet_thi, parent,
                    false);
            return new ItemViewHolder(view);
//        }

//        else if (viewType == TYPE_FOOTER) {
//            ViewHolder view = LayoutInflater.from(mContext).inflate(R.layout.item_diet_thi_foot, parent,
//                    false);
//
//            return new FootViewHolder(view);
//        }
//        return null;

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final ItemViewHolder viewHolder = (ItemViewHolder) holder;
        final DietThiModule.DataBean dataBean = data.get(position);
        viewHolder.title.setText(dataBean.getName());
        viewHolder.summary.setText(dataBean.getShort_desc());
//        Picasso.with(mContext).load(dataBean.getPic_path()).into(viewHolder.img);
        ImageLoader.picasso(dataBean.getPic_path(), viewHolder.img);
        viewHolder.clickLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, DietForActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(Constant.DIETTITLE, title);
                bundle.putSerializable(Constant.DIETDATA, dataBean);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
//        if (position + 1 == getItemCount()) {
//            return TYPE_FOOTER;
//        } else {
            return TYPE_ITEM;
//        }
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder{

        TextView title, summary;
        ImageView img;
        RelativeLayout clickLayout;

        public ItemViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.head_title);
            summary = (TextView) itemView.findViewById(R.id.summary);
            img = (ImageView) itemView.findViewById(R.id.img);
            clickLayout = (RelativeLayout) itemView.findViewById(R.id.layout);
        }
    }

//    static class FootViewHolder extends RecyclerView.ViewHolderOne {
//
//        public FootViewHolder(ViewHolder view) {
//            super(view);
//        }
//    }
}
